package com.ecy.quadratic.ui.fragment;

import android.support.v4.view.ViewPager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.ecy.quadratic.R;
import com.ecy.quadratic.adapter.ImagePagerAdapter;
import com.ecy.quadratic.adapter.VideoOneThirdAdapter;
import com.ecy.quadratic.beans.PageInfo;
import com.ecy.quadratic.beans.Video;
import com.ecy.quadratic.component.DaggerVideoMainComponent;
import com.ecy.quadratic.reader.base.BaseFragment;
import com.ecy.quadratic.reader.component.AppComponent;
import com.ecy.quadratic.ui.activity.VideoPlayActivity;
import com.ecy.quadratic.ui.contract.VideoListContract;
import com.ecy.quadratic.ui.presenter.VideoListPresenter;
import com.ecy.quadratic.widget.SwipeRefreshView;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.Bind;
import cn.trinea.android.view.autoscrollviewpager.AutoScrollViewPager;


/**
 * Created by Administrator on 2016/3/31.
 */
public class VideoMainFragment extends BaseFragment implements VideoListContract.View {

    @Bind(R.id.recommend_swiperefresh_layout)
    SwipeRefreshView mSwipeRefreshView;
    @Bind(R.id.activity_grid_view__main)
    LinearLayout mContentLlt;
    @Bind(R.id.recommend_gridview)
    GridView mGridView;
    @Bind(R.id.recommend_top_viewpager)
    AutoScrollViewPager mViewPager;
    @Bind(R.id.dot_0)
    View dot_0_view;
    @Bind(R.id.dot_1)
    View dot_1_view;
    @Bind(R.id.dot_2)
    View dot_2_View;
    @Bind(R.id.dot_3)
    View dot_3_View;
    @Bind(R.id.dot_4)
    View dot_4_View;

    @Inject
    VideoListPresenter mRecommendPresenter;

    private VideoOneThirdAdapter mRecommendAdapter;
    private List<Video> mComicList;
    private int mCurrentPage = 0;
    private int mTotalPage = 0;


    private List<ImageView> images;
    private List<View> dots;
    //记录上一次点的位置
    private int oldPosition = 0;
    private SwipeRefreshLayout.OnRefreshListener mOnRefreshListener = new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            mRecommendPresenter.refresh("1");
        }
    };

    public VideoMainFragment() {

    }

    @Override
    public int getLayoutResId() {
        return R.layout.fragment_video_main;
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerVideoMainComponent.builder()
                .appComponent(appComponent)
                .build()
                .inject(this);
    }

    @Override
    public void attachView() {
        if (mRecommendPresenter != null) {
            mRecommendPresenter.attachView(this);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mRecommendPresenter != null)
            mRecommendPresenter.detachView();
    }

    @Override
    public void initData() {
        mComicList = new ArrayList<>();
        mRecommendAdapter = new VideoOneThirdAdapter(getContext(), mComicList);
    }

    @Override
    public void configViews() {
        mSwipeRefreshView.setProgressBackgroundColorSchemeResource(android.R.color.white);
        mSwipeRefreshView.setColorSchemeResources(R.color.hobby_theme_color, R.color.hobby_theme_color, R.color.hobby_theme_color);
        mSwipeRefreshView.setOnRefreshListener(mOnRefreshListener);
        mSwipeRefreshView.post(new Runnable() {
            @Override
            public void run() {
                mSwipeRefreshView.setRefreshing(true);
                mOnRefreshListener.onRefresh();
            }
        });
        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                VideoPlayActivity.startActivity(getContext(), mComicList.get(position).getSectionlisturl());
//				ComicDetailActivity.startActivity(getContext(),"83772");//mRecommendAdapter.getItem(position).getId());
            }
        });

        mGridView.setAdapter(mRecommendAdapter);
        final ArrayList<Integer> list = new ArrayList<Integer>();
        list.add(R.drawable.c);
        list.add(R.drawable.b);
        list.add(R.drawable.c);
        list.add(R.drawable.d);
        list.add(R.drawable.b);

        mViewPager.setAdapter(new ImagePagerAdapter(getContext(), list).setInfiniteLoop(true));
        mViewPager.setScrollDurationFactor(3);
        mViewPager.setInterval(3000);
        mViewPager.startAutoScroll();
        mViewPager.setCurrentItem(Integer.MAX_VALUE / 2 - Integer.MAX_VALUE / 2 % list.size());
        //显示的小点
        dots = new ArrayList<View>();
        dots.add(dot_0_view);
        dots.add(dot_1_view);
        dots.add(dot_2_View);
        dots.add(dot_3_View);
        dots.add(dot_4_View);

        mViewPager.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_MOVE:
                        mSwipeRefreshView.setEnabled(false);
                        break;
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_CANCEL:
                        mSwipeRefreshView.setEnabled(true);
                }
                return false;
            }
        });
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {


            @Override
            public void onPageSelected(int position) {
                position = position % list.size();
                dots.get(position).setBackgroundResource(R.drawable.dot_focused);
                dots.get(oldPosition).setBackgroundResource(R.drawable.dot_normal);

                oldPosition = position;
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }
        });
    }

    /**
     * 利用线程池定时执行动画轮播
     */
    @Override
    public void onStart() {
        super.onStart();
        mViewPager.startAutoScroll();
    }

    @Override
    public void onStop() {
        super.onStop();
        mViewPager.stopAutoScroll();
    }

    @Override
    public void showRefreshList(PageInfo<Video> pageInfo) {
        mContentLlt.setVisibility(View.VISIBLE);
        if (pageInfo != null) {
            mCurrentPage = pageInfo.pageNum;
            mTotalPage = pageInfo.pages;
            mComicList.clear();
            mComicList.addAll(pageInfo.list);
            mRecommendAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void showLoadMoreList(PageInfo<Video> pageInfo) {
        if (pageInfo != null) {
            mCurrentPage = pageInfo.pageNum;
            mTotalPage = pageInfo.pages;
            mComicList.addAll(pageInfo.list);
            mRecommendAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void showError() {
        mSwipeRefreshView.setRefreshing(false);
        mSwipeRefreshView.setLoading(false, true);
    }

    @Override
    public void complete() {
        mSwipeRefreshView.setRefreshing(false);
        mSwipeRefreshView.setLoading(false);
    }
}
